import {
  knowledgeClassification,
  knowledgeAiLearningTree,
  saveQA,
  getKnowledgeDetail,
  getQAJsonAnswers,
  getAlikeQuestionList,
  getStandardQuestion,
  editQa,
  fetchSlotDetailBySlotId,
  getQAHistories,
  addKnowledge,
  addKnowledgeCluster,
  addKnowledgeAuto,
} from '@/services/knowledgemanagement';
import {
  fetchApiListById,
  slotList,
  fetchApiDetailById,
  fetchDetailCustomBySlotId,
} from '@/services/basiccapacity';

export default {
  namespace: 'editingKnowledge',
  state: {
    total: null,
    items: [],
    loading: false,
  },

  effects: {
    *getCategoryKnowledge({ payload, callback }, { call, put }) {
      const response = yield call(knowledgeClassification, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getAiLearningKnowledge({ payload, callback }, { call, put }) {
      const response = yield call(knowledgeAiLearningTree, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doSave({ payload, callback }, { call, put }) {
      const response = yield call(saveQA, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doEdit({ payload, callback }, { call, put }) {
      const response = yield call(editQa, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchKnowledgeDetail({ payload, callback }, { call, put }) {
      const response = yield call(getKnowledgeDetail, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchAnswerDetail({ payload, callback }, { call, put }) {
      const response = yield call(getQAJsonAnswers, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchSimiliarQuestion({ payload, callback }, { call, put }) {
      const response = yield call(getAlikeQuestionList, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchStandQuestion({ payload, callback }, { call, put }) {
      const response = yield call(getStandardQuestion, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchApiList({ payload, callback }, { call, put }) {
      const response = yield call(fetchApiListById, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchSlotList({ payload, callback }, { call, put }) {
      const response = yield call(slotList, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doApiDetailById({ payload, callback }, { call, put }) {
      const response = yield call(fetchApiDetailById, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getSlotDetail({ payload, callback }, { call, put }) {
      const response = yield call(fetchDetailCustomBySlotId, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchHistoryByQaId({ payload, callback }, { call, put }) {
      const response = yield call(getQAHistories, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *addKnowledgeByCapacity({ payload, callback }, { call, put }) {
      const response = yield call(addKnowledge, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *addKnowledgeByCluster({ payload, callback }, { call, put }) {
      const response = yield call(addKnowledgeCluster, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *addKnowledgeByAuto({ payload, callback }, { call, put }) {
      const response = yield call(addKnowledgeAuto, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
